<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload_control extends CI_Controller {
	
	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->load->model('upload_model');
		$this->load->model('download_model');
		$this->load->library('session');
	}

	function index($sid)
	{	
		if ( !$this->session->userdata('logged_in') ) {
		
			redirect('projekt/logged');
		
		}
		$mid = $this->session->flashdata('mid');
		$links = $this->download_model->get_links($sid);

		$data = array(
				'mid' => $mid,
				'sid' => $sid
			);
		$this->session->set_flashdata($data);
		$this->session->keep_flashdata($data);
		$obrazky = array_diff(scandir('./images'), array('..', '.'));

		$this->load->view('upload', array('mid' => $mid, 'obrazky' => $obrazky, 'links' => $links));
	}

	function do_upload()
	{	

		if (!$this->session->userdata('logged_in')) {
		
			redirect('projekt/logged');
		
		}
		
		$config['upload_path'] = './images';
		$config['allowed_types'] = 'gif|jpg|png';

		$this->load->library('upload', $config);

		$mid = $this->session->flashdata('mid');
		$sid = $this->session->flashdata('sid');
		$data = array(
				'mid' => $mid,
				'sid' => $sid
			);
		$this->session->set_flashdata($data);
		$this->session->keep_flashdata($data);
		$links = $this->download_model->get_links($sid);

		if ( ! $this->upload->do_upload() )
		{	
			$error = $this->upload->display_errors();
			$this->session->keep_flashdata($data);

			$obrazky = $this->download_model->get_img($sid);

			$this->load->view('upload', array('error' => $error, 'mid' => $mid, 'obrazky' => $obrazky, 'links' => $links));
		}
		else
		{	
			$obrazok = $this->upload->data();

			$cesta = 'http://kempelen.ii.fmph.uniba.sk/letnaliga/images/'.$obrazok['file_name'];

			chmod('./images/'.$obrazok['file_name'], 0755);

			$this->upload_model->upload_img($sid, $obrazok['file_name']);

			$string = 'Obrázok úspešne načítaný!<br/>Nahrajte ďalší obrázok, alebo stlačte Uložiť.';

			$mid = $this->session->flashdata('mid');
			$sid = $this->session->flashdata('sid');
			$data = array(
				'mid' => $mid,
				'sid' => $sid
			);
			$this->session->set_flashdata($data);
			$this->session->keep_flashdata($data);

			$obrazky = $this->download_model->get_img($sid);

			$links = $this->download_model->get_links($sid);

			$this->load->view('upload', array('error' => $string, 'mid' => $mid, 'obrazky' => $obrazky, 'links' => $links));
		}
	}

	function delete_image($img)
	{
		if (!$this->session->userdata('logged_in')) {
		
			redirect('projekt/logged');
		
		}

		$mid = $this->session->flashdata('mid');
		$sid = $this->session->flashdata('sid');
		$data = array(
				'mid' => $mid,
				'sid' => $sid
		);
		$this->session->set_flashdata($data);
		$this->session->keep_flashdata($data);
		unlink('./images/'.$img);
		$this->upload_model->delete_img($sid, $img);

		$obrazky = $this->download_model->get_img($sid);
		$string = 'Obrázok vymazaný.';
		$links = $this->download_model->get_links($sid);


		$this->load->view('upload', array('error' => $string, 'mid' => $mid, 'obrazky' => $obrazky, 'links' => $links));
	}


	function upload_url()
	{	
		if (!$this->session->userdata('logged_in')) {
		
			redirect('projekt/logged');
		
		}
		$config2 = array(

				array(
                     'field'   => 'link', 
                     'label'   => 'Url', 
                     'rules'   => 'required'
                  )

			);
		$this->load->library('form_validation');
		$this->form_validation->set_rules($config2);

		if ( $this->form_validation->run() == FALSE )
		{
			$mid = $this->session->flashdata('mid');
			$sid = $this->session->flashdata('sid');
			$data = array(
					'mid' => $mid,
					'sid' => $sid
			);
			$this->session->set_flashdata($data);
			$this->session->keep_flashdata($data);

			$links = $this->download_model->get_links($sid);


			$obrazky = $this->download_model->get_img($sid);

			$this->load->view('upload', array('error' => '', 'mid' => $mid, 'obrazky' => $obrazky, 'links' => $links));
		} else {
			$mid = $this->session->flashdata('mid');
			$sid = $this->session->flashdata('sid');
			$data = array(
					'mid' => $mid,
					'sid' => $sid
			);
			$this->session->set_flashdata($data);
			$this->session->keep_flashdata($data);

			$vstup = $this->input->post('link');
			$vystup = substr($vstup, 0, strpos($vstup, 'watch?v=')) . 'embed/' . substr($vstup, strpos($vstup, 'watch?v=') + 8);
			$this->upload_model->upload_link($sid, $vystup);
			$links = $this->download_model->get_links($sid);


			$obrazky = $this->download_model->get_img($sid);
			$string = 'Video pridané.';
			$this->load->view('upload', array('error' => $string, 'mid' => $mid, 'obrazky' => $obrazky, 'links' => $links));
		}
	}	

	function delete_url($id)
	{	
		if (!$this->session->userdata('logged_in')) {
		
			redirect('projekt/logged');
		
		}
			$mid = $this->session->flashdata('mid');
			$sid = $this->session->flashdata('sid');
			$data = array(
					'mid' => $mid,
					'sid' => $sid
			);
			$this->session->set_flashdata($data);
			$this->session->keep_flashdata($data);
			$this->upload_model->delete_link($sid, $id);

			$links = $this->download_model->get_links($sid);


			$obrazky = $this->download_model->get_img($sid);
			$string = 'Video vymazané.';
			$this->load->view('upload', array('error' => $string, 'mid' => $mid, 'obrazky' => $obrazky, 'links' => $links));
		}
}
?>
